*================================================================================
* WHAT  ON  EARTH!
* Assigning BEA country name and Region names
*================================================================================


/* ****NOTE: the shared area is the concordance between BEA and LFTTD
*            NOT Needed here
*================================================================================
* Concordance between BEA country code and census country code
* Assigning BEA country name
*================================================================================
*/

**Set directories
cd ""  /* PROJECT ROOT FOLDER */
global data "data"
global input "input/country_data_input"


use $input/country_data.dta, clear
rename country country_name

 
*1) Fill in missing GDP for Taiwan
********************************************
replace gdp=408220000000 if country_name=="Taiwan"  //  number from CEPII dataset
********************************************


*2)  Make country percentile measures
********************************************
  xtile gdp_qtile=gdp, nq(5)
  gen gdp_pc=gdp/pop_o
    replace gdp_pc=gdp/(pop/1000) if gdp_pc==.
  xtile gdp_pc_qtile=gdp_pc, nq(5)
******************************************** 


*3)  Fill in some missing info for Netherlands-Antilles
********************************************
  replace iso3="ANT" if code_TD=="2771"  // public data from the FTD website
  *add more info here it if seems important
********************************************

*4) =======ASSIGN BEA COUNTRY CODE TO CENSUS COUNTRY CODE ========================
	* use  'countrycode_lfttd' (census)  to create 'countrycode' (BEA)
  *use $input/country_data.dta, clear
  *rename country country_name
gen countrycode_lfttd=code_TD
drop if code_TD=="."	
	
ge countrycode=.					
	replace countrycode=199    if countrycode_lfttd=="1000"
	replace countrycode=311    if countrycode_lfttd=="1010"
	replace countrycode=100    if countrycode_lfttd=="1220"
	replace countrycode=263    if countrycode_lfttd=="1610"
	replace countrycode=213    if countrycode_lfttd=="2010"
	replace countrycode=210    if countrycode_lfttd=="2050"
	replace countrycode=254    if countrycode_lfttd=="2080"
	replace countrycode=209    if countrycode_lfttd=="2110"
	replace countrycode=212    if countrycode_lfttd=="2150"
	replace countrycode=214    if countrycode_lfttd=="2190"
	replace countrycode=205    if countrycode_lfttd=="2230"
	replace countrycode=215    if countrycode_lfttd=="2250"
	replace countrycode=252    if countrycode_lfttd=="2320"
	replace countrycode=250    if countrycode_lfttd=="2360"
	replace countrycode=206    if countrycode_lfttd=="2390"
	replace countrycode=258    if countrycode_lfttd=="2410"
	replace countrycode=266    if countrycode_lfttd=="2430"
	replace countrycode=266    if countrycode_lfttd=="2440"
	replace countrycode=211    if countrycode_lfttd=="2450"
	replace countrycode=207    if countrycode_lfttd=="2470"
	replace countrycode=272    if countrycode_lfttd=="2481"
	replace countrycode=266    if countrycode_lfttd=="2482"
	replace countrycode=267    if countrycode_lfttd=="2483"
	replace countrycode=273    if countrycode_lfttd=="2484"
	replace countrycode=266    if countrycode_lfttd=="2485"
	replace countrycode=269    if countrycode_lfttd=="2486"
	replace countrycode=270    if countrycode_lfttd=="2487"
	replace countrycode=271    if countrycode_lfttd=="2488"
	replace countrycode=265    if countrycode_lfttd=="2489"
	replace countrycode=251    if countrycode_lfttd=="2720"
	replace countrycode=261    if countrycode_lfttd=="2740"
	replace countrycode=259    if countrycode_lfttd=="2771"
	replace countrycode=276    if countrycode_lfttd=="2774"
	replace countrycode=275    if countrycode_lfttd=="2777"
	replace countrycode=274    if countrycode_lfttd=="2779"
	replace countrycode=255    if countrycode_lfttd=="2831"
	replace countrycode=255    if countrycode_lfttd=="2839"
	replace countrycode=204    if countrycode_lfttd=="3010"
	replace countrycode=219    if countrycode_lfttd=="3070"
	replace countrycode=257    if countrycode_lfttd=="3120"
	replace countrycode=260    if countrycode_lfttd=="3150"
	replace countrycode=256    if countrycode_lfttd=="3170"
	replace countrycode=208    if countrycode_lfttd=="3310"
	replace countrycode=217    if countrycode_lfttd=="3330"
	replace countrycode=201    if countrycode_lfttd=="3350"
	replace countrycode=203    if countrycode_lfttd=="3370"
	replace countrycode=202    if countrycode_lfttd=="3510"
	replace countrycode=216    if countrycode_lfttd=="3530"
	replace countrycode=218    if countrycode_lfttd=="3550"
	replace countrycode=200    if countrycode_lfttd=="3570"
	replace countrycode=268    if countrycode_lfttd=="3720"
	replace countrycode=312    if countrycode_lfttd=="4000"
	replace countrycode=324    if countrycode_lfttd=="4010"
	replace countrycode=320    if countrycode_lfttd=="4031"
	replace countrycode=320    if countrycode_lfttd=="4039"
	replace countrycode=306    if countrycode_lfttd=="4050"
	replace countrycode=305    if countrycode_lfttd=="4091"
	replace countrycode=305    if countrycode_lfttd=="4099"
	replace countrycode=327    if countrycode_lfttd=="4120"
	replace countrycode=313    if countrycode_lfttd=="4190"
	replace countrycode=319    if countrycode_lfttd=="4210"
	replace countrycode=302    if countrycode_lfttd=="4231"
	replace countrycode=316    if countrycode_lfttd=="4239"
	replace countrycode=300    if countrycode_lfttd=="4271"
	replace countrycode=318    if countrycode_lfttd=="4272"
	replace countrycode=307    if countrycode_lfttd=="4279"
	replace countrycode=308    if countrycode_lfttd=="4280"
	replace countrycode=301    if countrycode_lfttd=="4330"
	replace countrycode=364    if countrycode_lfttd=="4351"
	replace countrycode=365    if countrycode_lfttd=="4359"
	replace countrycode=354    if countrycode_lfttd=="4370"
	replace countrycode=315    if countrycode_lfttd=="4411"
	replace countrycode=325    if countrycode_lfttd=="4419"
	replace countrycode=331    if countrycode_lfttd=="4470"
	replace countrycode=332    if countrycode_lfttd=="4490"
	replace countrycode=333    if countrycode_lfttd=="4510"
	replace countrycode=355    if countrycode_lfttd=="4550"
	replace countrycode=341    if countrycode_lfttd=="4621"
	replace countrycode=336    if countrycode_lfttd=="4622"
	replace countrycode=344    if countrycode_lfttd=="4623"
	replace countrycode=334    if countrycode_lfttd=="4631"
	replace countrycode=335    if countrycode_lfttd=="4632"
	replace countrycode=337    if countrycode_lfttd=="4633"
	replace countrycode=338    if countrycode_lfttd=="4634"
	replace countrycode=339    if countrycode_lfttd=="4635"
	replace countrycode=340    if countrycode_lfttd=="4641"
	replace countrycode=342    if countrycode_lfttd=="4642"
	replace countrycode=343    if countrycode_lfttd=="4643"
	replace countrycode=345    if countrycode_lfttd=="4644"
	replace countrycode=323    if countrycode_lfttd=="4700"
	replace countrycode=321    if countrycode_lfttd=="4710"
	replace countrycode=309    if countrycode_lfttd=="4720"
	replace countrycode=317    if countrycode_lfttd=="4730"
	replace countrycode=322    if countrycode_lfttd=="4751"
	replace countrycode=330    if countrycode_lfttd=="4752"
	replace countrycode=314    if countrycode_lfttd=="4759"
	replace countrycode=359    if countrycode_lfttd=="4791"
	replace countrycode=363    if countrycode_lfttd=="4792"
	replace countrycode=358    if countrycode_lfttd=="4793"
	replace countrycode=360    if countrycode_lfttd=="4794"
	replace countrycode=362    if countrycode_lfttd=="4801"
	replace countrycode=366    if countrycode_lfttd=="4803"
	replace countrycode=361    if countrycode_lfttd=="4804"
	replace countrycode=350    if countrycode_lfttd=="4810"
	replace countrycode=310    if countrycode_lfttd=="4840"
	replace countrycode=356    if countrycode_lfttd=="4850"
	replace countrycode=351    if countrycode_lfttd=="4870"
	replace countrycode=326    if countrycode_lfttd=="4890"
	replace countrycode=304    if countrycode_lfttd=="4910"
	replace countrycode=512    if countrycode_lfttd=="5020"
	replace countrycode=507    if countrycode_lfttd=="5040"
	replace countrycode=503    if countrycode_lfttd=="5050"
	replace countrycode=502    if countrycode_lfttd=="5070"
	replace countrycode=504    if countrycode_lfttd=="5081"
	replace countrycode=504    if countrycode_lfttd=="5082"
	replace countrycode=504    if countrycode_lfttd=="5083"
	replace countrycode=505    if countrycode_lfttd=="5110"
	replace countrycode=506    if countrycode_lfttd=="5130"
	replace countrycode=511    if countrycode_lfttd=="5170"
	replace countrycode=510    if countrycode_lfttd=="5180"
	replace countrycode=513    if countrycode_lfttd=="5200"
	replace countrycode=514    if countrycode_lfttd=="5210"
	replace countrycode=508    if countrycode_lfttd=="5230"
	replace countrycode=501    if countrycode_lfttd=="5250"
	replace countrycode=600    if countrycode_lfttd=="5310"
	replace countrycode=612    if countrycode_lfttd=="5330"
	replace countrycode=622    if countrycode_lfttd=="5350"
	replace countrycode=618    if countrycode_lfttd=="5360"
	replace countrycode=631    if countrycode_lfttd=="5380"
	replace countrycode=608    if countrycode_lfttd=="5420"
	replace countrycode=606    if countrycode_lfttd=="5460"
	replace countrycode=629    if countrycode_lfttd=="5490"
	replace countrycode=653    if countrycode_lfttd=="5520"
	replace countrycode=615    if countrycode_lfttd=="5530"
	replace countrycode=607    if countrycode_lfttd=="5550"
	replace countrycode=617    if countrycode_lfttd=="5570"
	replace countrycode=625    if countrycode_lfttd=="5590"
	replace countrycode=613    if countrycode_lfttd=="5600"
	replace countrycode=660    if countrycode_lfttd=="5601"
	replace countrycode=603    if countrycode_lfttd=="5610"
	replace countrycode=623    if countrycode_lfttd=="5650"
	replace countrycode=616    if countrycode_lfttd=="5660"
	replace countrycode=602    if countrycode_lfttd=="5682"
	replace countrycode=633    if countrycode_lfttd=="5683"
	replace countrycode=650    if countrycode_lfttd=="5700"
	replace countrycode=651    if countrycode_lfttd=="5740"
	replace countrycode=652    if countrycode_lfttd=="5790"
	replace countrycode=626    if countrycode_lfttd=="5800"
	replace countrycode=611    if countrycode_lfttd=="5820"
	replace countrycode=628    if countrycode_lfttd=="5830"
	replace countrycode=614    if countrycode_lfttd=="5880"
	replace countrycode=601    if countrycode_lfttd=="6021"
	replace countrycode=601    if countrycode_lfttd=="6022"
	replace countrycode=601    if countrycode_lfttd=="6023"
	replace countrycode=655    if countrycode_lfttd=="6024"
	replace countrycode=601    if countrycode_lfttd=="6029"
	replace countrycode=619    if countrycode_lfttd=="6040"
	replace countrycode=620    if countrycode_lfttd=="6141"
	replace countrycode=620    if countrycode_lfttd=="6142"
	replace countrycode=620    if countrycode_lfttd=="6143"
	replace countrycode=620    if countrycode_lfttd=="6144"
	replace countrycode=637    if countrycode_lfttd=="6150"
	replace countrycode=654    if countrycode_lfttd=="6223"
	replace countrycode=636    if countrycode_lfttd=="6224"
	replace countrycode=604    if countrycode_lfttd=="6225"
	replace countrycode=655    if countrycode_lfttd=="6226"
	replace countrycode=656    if countrycode_lfttd=="6227"
	replace countrycode=610    if countrycode_lfttd=="6412"
	replace countrycode=610    if countrycode_lfttd=="6413"
	replace countrycode=610    if countrycode_lfttd=="6414"
	replace countrycode=658    if countrycode_lfttd=="6810"
	replace countrycode=657    if countrycode_lfttd=="6820"
	replace countrycode=659    if countrycode_lfttd=="6830"
	replace countrycode=635    if countrycode_lfttd=="6862"
	replace countrycode=632    if countrycode_lfttd=="6863"
	replace countrycode=638    if countrycode_lfttd=="6864"
	replace countrycode=426    if countrycode_lfttd=="7140"
	replace countrycode=400    if countrycode_lfttd=="7210"
	replace countrycode=445    if countrycode_lfttd=="7230"
	replace countrycode=421    if countrycode_lfttd=="7250"
	replace countrycode=410    if countrycode_lfttd=="7290"
	replace countrycode=441    if countrycode_lfttd=="7321"
	replace countrycode=458    if countrycode_lfttd=="7323"
	replace countrycode=440    if countrycode_lfttd=="7370"
	replace countrycode=438    if countrycode_lfttd=="7380"
	replace countrycode=425    if countrycode_lfttd=="7410"
	replace countrycode=404    if countrycode_lfttd=="7420"
	replace countrycode=433    if countrycode_lfttd=="7440"
	replace countrycode=424    if countrycode_lfttd=="7450"
	replace countrycode=416    if countrycode_lfttd=="7460"
	replace countrycode=434    if countrycode_lfttd=="7470"
	replace countrycode=417    if countrycode_lfttd=="7480"
	replace countrycode=415    if countrycode_lfttd=="7490"
	replace countrycode=414    if countrycode_lfttd=="7500"
	replace countrycode=428    if countrycode_lfttd=="7510"
	replace countrycode=444    if countrycode_lfttd=="7520"
	replace countrycode=429    if countrycode_lfttd=="7530"
	replace countrycode=405    if countrycode_lfttd=="7540"
	replace countrycode=413    if countrycode_lfttd=="7550"
	replace countrycode=406    if countrycode_lfttd=="7560"
	replace countrycode=455    if countrycode_lfttd=="7580"
	replace countrycode=447    if countrycode_lfttd=="7600"
	replace countrycode=409    if countrycode_lfttd=="7610"
	replace countrycode=401    if countrycode_lfttd=="7620"
	replace countrycode=407    if countrycode_lfttd=="7630"
	replace countrycode=430    if countrycode_lfttd=="7642"
	replace countrycode=450    if countrycode_lfttd=="7643"
	replace countrycode=451    if countrycode_lfttd=="7644"
	replace countrycode=420    if countrycode_lfttd=="7650"
	replace countrycode=408    if countrycode_lfttd=="7660"
	replace countrycode=403    if countrycode_lfttd=="7670"
	replace countrycode=432    if countrycode_lfttd=="7690"
	replace countrycode=435    if countrycode_lfttd=="7700"
	replace countrycode=457    if countrycode_lfttd=="7741"
	replace countrycode=411    if countrycode_lfttd=="7749"
	replace countrycode=412    if countrycode_lfttd=="7770"
	replace countrycode=446    if countrycode_lfttd=="7780"
	replace countrycode=418    if countrycode_lfttd=="7790"
	replace countrycode=454    if countrycode_lfttd=="7800"
	replace countrycode=605    if countrycode_lfttd=="7810"
	replace countrycode=443    if countrycode_lfttd=="7830"
	replace countrycode=453    if countrycode_lfttd=="7850"
	replace countrycode=427    if countrycode_lfttd=="7870"
	replace countrycode=422    if countrycode_lfttd=="7880"
	replace countrycode=456    if countrycode_lfttd=="7881"
	replace countrycode=456    if countrycode_lfttd=="7890"
	replace countrycode=609    if countrycode_lfttd=="7904"
	replace countrycode=609    if countrycode_lfttd=="7905"
	replace countrycode=436    if countrycode_lfttd=="7910"
	replace countrycode=437    if countrycode_lfttd=="7920"
	replace countrycode=402    if countrycode_lfttd=="7930"
	replace countrycode=448    if countrycode_lfttd=="7940"
	replace countrycode=442    if countrycode_lfttd=="7950"
	replace countrycode=431    if countrycode_lfttd=="7960"
	replace countrycode=423    if countrycode_lfttd=="7970"
	replace countrycode=419    if countrycode_lfttd=="7990"
	replace countrycode=199    if countrycode_lfttd=="9030"
	replace countrycode=199    if countrycode_lfttd=="9110"
	replace countrycode=199    if countrycode_lfttd=="9350"
	replace countrycode=199    if countrycode_lfttd=="9510"
	replace countrycode=199    if countrycode_lfttd=="9610"
	replace countrycode=199    if countrycode_lfttd=="9800"


** ASSIGN BEA COUNTRY NAMES TO BEA COUNTRY CODES ==================
	* Use 'countrycode' (BEA) to generate 'country'
ge country=""			
replace country="Afghanistan"  if countrycode==600
replace country="Albania"  if countrycode==350
replace country="Algeria"  if countrycode==400
replace country="Andorra"  if countrycode==300
replace country="Angola"  if countrycode==401
replace country="Anguilla"  if countrycode==272
replace country="Antigua and Barbuda"  if countrycode==273
replace country="Argentina"  if countrycode==200
replace country="Armenia"  if countrycode==334
replace country="Aruba"  if countrycode==274
replace country="UK Islands Atlantic (Africa)"  if countrycode==455
replace country="Australia"  if countrycode==601
replace country="Australia"  if countrycode==601
replace country="Austria"  if countrycode==301
replace country="Azerbaijan"  if countrycode==335
replace country="Portugal"  if countrycode==321
replace country="Bahamas"  if countrycode==250
replace country="Bahrain"  if countrycode==501
replace country="Bangladesh"  if countrycode==631
replace country="Barbados"  if countrycode==251
replace country="Belarus"  if countrycode==336
replace country="Belgium"  if countrycode==302
replace country="Belize"  if countrycode==254
replace country="Benin"  if countrycode==409
replace country="Bermuda"  if countrycode==252
replace country="Bhutan"  if countrycode==602
replace country="Bolivia"  if countrycode==201
replace country="Netherlands Islands- Caribbean "  if countrycode==277
replace country="Japan"  if countrycode==614
replace country="Bosnia and Herzegovina"  if countrycode==358
replace country="Botswana"  if countrycode==402
replace country="Norway"  if countrycode==320
replace country="Brazil"  if countrycode==202
replace country="UK Islands Caribbean"  if countrycode==266
replace country="UK Islands Caribbean"  if countrycode==266
replace country="Brunei"  if countrycode==603
replace country="Bulgaria"  if countrycode==351
replace country="Burkina Faso"  if countrycode==447
replace country="Burma"  if countrycode==606
replace country="Burundi"  if countrycode==403
replace country="Angola"  if countrycode==401
replace country="Cabo Verde"  if countrycode==450
replace country="Cambodia"  if countrycode==607
replace country="Cameroon"  if countrycode==404
replace country="Canada"  if countrycode==100
replace country="Spain"  if countrycode==323
replace country="Kiribati"  if countrycode==655
replace country="UK Islands Caribbean"  if countrycode==266
replace country="Central African Republic"  if countrycode==405
replace country="Chad"  if countrycode==406
replace country="United Kingdom"  if countrycode==327
replace country="Chile"  if countrycode==203
replace country="China"  if countrycode==650
replace country="Kiribati"  if countrycode==655
replace country="French Islands Pacific"  if countrycode==610
replace country="Australia"  if countrycode==601
replace country="Colombia"  if countrycode==204
replace country="Comoros"  if countrycode==456
replace country="Congo, Democratic Republic of"  if countrycode==408
replace country="Congo, Republic of"  if countrycode==407
replace country="New Zealand"  if countrycode==620
replace country="Australia"  if countrycode==601
replace country="Costa Rica"  if countrycode==205
replace country="Cote d'Ivoire"  if countrycode==417
replace country="Croatia"  if countrycode==359
replace country="Cuba"  if countrycode==206
replace country="Curacao"  if countrycode==275
replace country="Cyprus"  if countrycode==304
replace country="Czech Republic"  if countrycode==364
replace country="Denmark"  if countrycode==305
replace country="Djibouti"  if countrycode==412
replace country="Dominica"  if countrycode==269
replace country="Dominican Republic"  if countrycode==207
replace country="UK Islands Atlantic"  if countrycode==268
replace country="Ecuador"  if countrycode==208
replace country="Egypt"  if countrycode==410
replace country="El Salvador"  if countrycode==209
replace country="United Kingdom"  if countrycode==327
replace country="Equatorial Guinea"  if countrycode==438
replace country="Eritrea"  if countrycode==457
replace country="Estonia"  if countrycode==331
replace country="Ethiopia"  if countrycode==411
replace country="French Islands Indian Ocean"  if countrycode==609
replace country="UK Islands Atlantic"  if countrycode==268
replace country="Denmark"  if countrycode==305
replace country="Fiji"  if countrycode==632
replace country="Finland"  if countrycode==306
replace country="French Islands Caribbean"  if countrycode==255
replace country="France"  if countrycode==307
replace country="French Islands Caribbean"  if countrycode==255
replace country="French Guiana"  if countrycode==256
replace country="French Islands Caribbean"  if countrycode==255
replace country="French Islands Indian Ocean"  if countrycode==609
replace country="French Islands Pacific"  if countrycode==610
replace country="French Islands Pacific"  if countrycode==610
replace country="French Islands Indian Ocean"  if countrycode==609
replace country="Gabon"  if countrycode==413
replace country="Gambia"  if countrycode==414
replace country="Israel"  if countrycode==504
replace country="Georgia"  if countrycode==337
replace country="Germany"  if countrycode==308
replace country="Ghana"  if countrycode==415
replace country="Gibraltar"  if countrycode==309
replace country="United Kingdom"  if countrycode==327
replace country="Greece"  if countrycode==310
replace country="Greenland"  if countrycode==311
replace country="Grenada"  if countrycode==265
replace country="French Islands Caribbean"  if countrycode==255
replace country="Guatemala"  if countrycode==210
replace country="Guernsey"  if countrycode==327
replace country="Guinea"  if countrycode==416
replace country="Guinea-Bissau"  if countrycode==430
replace country="Guyana"  if countrycode==257
replace country="Haiti"  if countrycode==211
replace country="Australia"  if countrycode==601
replace country="Honduras"  if countrycode==212
replace country="Hong Kong"  if countrycode==611
replace country="Hungary"  if countrycode==354
replace country="Iceland"  if countrycode==312
replace country="India"  if countrycode==612
replace country="Indonesia"  if countrycode==613
replace country="Iran"  if countrycode==502
replace country="Iraq"  if countrycode==503
replace country="Ireland"  if countrycode==313
replace country="United Kingdom"  if countrycode==327
replace country="Israel"  if countrycode==504
replace country="Italy"  if countrycode==314
replace country="Jamaica"  if countrycode==258
replace country="Norway"  if countrycode==320
replace country="Japan"  if countrycode==614
replace country="United Kingdom"  if countrycode==327
replace country="Jordan"  if countrycode==505
replace country="French Islands Indian Ocean"  if countrycode==609
replace country="Kazakhstan"  if countrycode==338
replace country="Kenya"  if countrycode==418
replace country="Kiribati"  if countrycode==655
replace country="Korea, Republic of"  if countrycode==626
replace country="Kosovo"  if countrycode==366
replace country="Kuwait"  if countrycode==506
replace country="Kyrgyzstan"  if countrycode==339
replace country="Laos"  if countrycode==615
replace country="Latvia"  if countrycode==332
replace country="Lebanon"  if countrycode==507
replace country="Lesotho"  if countrycode==419
replace country="Liberia"  if countrycode==420
replace country="Libya"  if countrycode==421
replace country="Liechtenstein"  if countrycode==315
replace country="Lithuania"  if countrycode==333
replace country="Luxembourg"  if countrycode==316
replace country="Macau"  if countrycode==616
replace country="Macedonia"  if countrycode==360
replace country="Madagascar"  if countrycode==422
replace country="Portugal"  if countrycode==321
replace country="Malawi"  if countrycode==423
replace country="Malaysia"  if countrycode==617
replace country="Maldives"  if countrycode==633
replace country="Mali"  if countrycode==424
replace country="Malta"  if countrycode==317
replace country="Marshall Islands, Republic of"  if countrycode==658
replace country="French Islands Caribbean"  if countrycode==255
replace country="Mauritania"  if countrycode==425
replace country="Mauritius"  if countrycode==453
replace country="Comoros"  if countrycode==456
replace country="Mexico"  if countrycode==213
replace country="Micronesia, Federated States of"  if countrycode==657
replace country="Moldova"  if countrycode==340
replace country="Monaco"  if countrycode==318
replace country="Mongolia"  if countrycode==651
replace country="Montenegro"  if countrycode==361
replace country="UK Islands Caribbean"  if countrycode==266
replace country="Morocco"  if countrycode==426
replace country="Mozambique"  if countrycode==427
replace country="Namibia"  if countrycode==437
replace country="Nauru"  if countrycode==635
replace country="Nepal"  if countrycode==618
replace country="Netherlands"  if countrycode==319
replace country="Netherlands Antilles"  if countrycode==259
replace country="Netherlands Islands- Caribbean "  if countrycode==277
replace country="French Islands Pacific"  if countrycode==610
replace country="New Zealand"  if countrycode==620
replace country="Nicaragua"  if countrycode==214
replace country="Niger"  if countrycode==428
replace country="Nigeria"  if countrycode==429
replace country="New Zealand"  if countrycode==620
replace country="Australia"  if countrycode==601
replace country="North Korea"  if countrycode==652
replace country="United Kingdom"  if countrycode==327
replace country="Norway"  if countrycode==320
replace country="Oman"  if countrycode==508
replace country="Pakistan"  if countrycode==622
replace country="Palau"  if countrycode==659
replace country="Panama"  if countrycode==215
replace country="Papua New Guinea"  if countrycode==619
replace country="China"  if countrycode==650
replace country="Paraguay"  if countrycode==216
replace country="Peru"  if countrycode==217
replace country="Philippines"  if countrycode==623
replace country="UK Islands Pacific"  if countrycode==604
replace country="Poland"  if countrycode==355
replace country="Portugal"  if countrycode==321
replace country="Colombia"  if countrycode==204
replace country="Qatar"  if countrycode==510
replace country="Antigua and Barbuda"  if countrycode==273
replace country="French Islands Indian Ocean"  if countrycode==609
replace country="Romania"  if countrycode==356
replace country="Russia"  if countrycode==341
replace country="Rwanda"  if countrycode==432
replace country="Netherlands Islands- Caribbean "  if countrycode==277
replace country="Samoa"  if countrycode==637
replace country="San Marino"  if countrycode==322
replace country="Sao Tome and Principe"  if countrycode==451
replace country="Italy"  if countrycode==314
replace country="Saudi Arabia"  if countrycode==511
replace country="United Kingdom"  if countrycode==327
replace country="Senegal"  if countrycode==433
replace country="Serbia"  if countrycode==362
replace country="Seychelles"  if countrycode==454
replace country="United Kingdom"  if countrycode==327
replace country="Italy"  if countrycode==314
replace country="Sierra Leone"  if countrycode==434
replace country="Singapore"  if countrycode==625
replace country="Sint Maarten"  if countrycode==276
replace country="Slovakia"  if countrycode==365
replace country="Slovenia"  if countrycode==363
replace country="Solomon Islands"  if countrycode==654
replace country="Somalia"  if countrycode==435
replace country="South Africa"  if countrycode==436
replace country="UK Islands Atlantic"  if countrycode==268
replace country="UK Islands Atlantic"  if countrycode==268
replace country="UK Islands Atlantic"  if countrycode==268
replace country="South Sudan"  if countrycode==458
replace country="Spain"  if countrycode==323
replace country="China"  if countrycode==650
replace country="Sri Lanka"  if countrycode==608
replace country="French Islands Caribbean"  if countrycode==255
replace country="Netherlands Islands- Caribbean "  if countrycode==277
replace country="UK Islands Atlantic (Africa)"  if countrycode==455
replace country="St. Kitts and Nevis"  if countrycode==267
replace country="St. Lucia"  if countrycode==270
replace country="St. Pierre and Miquelon"  if countrycode==263
replace country="St. Vincent and the Grenadines"  if countrycode==271
replace country="Sudan"  if countrycode==441
replace country="Suriname"  if countrycode==260
replace country="Norway"  if countrycode==320
replace country="Swaziland"  if countrycode==442
replace country="Sweden"  if countrycode==324
replace country="Switzerland"  if countrycode==325
replace country="Syria"  if countrycode==512
replace country="Taiwan"  if countrycode==628
replace country="Tajikistan"  if countrycode==342
replace country="Tanzania"  if countrycode==443
replace country="Thailand"  if countrycode==629
replace country="Timor-Leste"  if countrycode==660
replace country="Togo"  if countrycode==444
replace country="New Zealand"  if countrycode==620
replace country="Tonga"  if countrycode==638
replace country="UK Islands Caribbean"  if countrycode==266
replace country="Trinidad and Tobago"  if countrycode==261
replace country="French Islands Indian Ocean"  if countrycode==609
replace country="Tunisia"  if countrycode==445
replace country="Turkey"  if countrycode==326
replace country="Turkmenistan"  if countrycode==343
replace country="UK Islands Caribbean"  if countrycode==266
replace country="Tuvalu"  if countrycode==656
replace country="Uganda"  if countrycode==446
replace country="UK Islands Atlantic"  if countrycode==268
replace country="UK Islands Atlantic (Africa)"  if countrycode==455
replace country="UK Islands Caribbean"  if countrycode==266
replace country="UK Islands Indian Ocean"  if countrycode==605
replace country="UK Islands Pacific"  if countrycode==604
replace country="Ukraine"  if countrycode==344
replace country="United Arab Emirates"  if countrycode==513
replace country="United Kingdom"  if countrycode==327
replace country="United States of America"  if countrycode==199
replace country="Uruguay"  if countrycode==218
replace country="Uzbekistan"  if countrycode==345
replace country="Vanuatu"  if countrycode==636
replace country="Vatican City"  if countrycode==330
replace country="Venezuela"  if countrycode==219
replace country="Vietnam"  if countrycode==653
replace country="United Kingdom"  if countrycode==327
replace country="French Islands Pacific"  if countrycode==610
replace country="Israel"  if countrycode==504
replace country="UK Islands Atlantic"  if countrycode==268
replace country="Western Sahara"  if countrycode==440
replace country="Yemen"  if countrycode==514
replace country="Yemen (Aden)"  if countrycode==500
replace country="Yugoslavia"  if countrycode==328
replace country="Zambia"  if countrycode==448
replace country="Zimbabwe"  if countrycode==431


** ASSIGN REGION CODES TO BEA COUNTRY CODES ==================================
	* Use 'countrycode' (BEA) to generate 'region'
ge region = ""
replace region="Asia"    if countrycode==600
replace region="Europe"    if countrycode==350
replace region="Africa"    if countrycode==400
replace region="Europe"    if countrycode==300
replace region="Africa"    if countrycode==401
replace region="OWH"    if countrycode==272
replace region="OWH"    if countrycode==273
replace region="SCA"    if countrycode==200
replace region="Europe"    if countrycode==334
replace region="OWH"    if countrycode==274
replace region="Africa"    if countrycode==455
replace region="Asia"    if countrycode==601
replace region="Asia"    if countrycode==601
replace region="Europe"    if countrycode==301
replace region="Europe"    if countrycode==335
replace region="Europe"    if countrycode==321
replace region="OWH"    if countrycode==250
replace region="Middle East"    if countrycode==501
replace region="Asia"    if countrycode==631
replace region="OWH"    if countrycode==251
replace region="Europe"    if countrycode==336
replace region="Europe"    if countrycode==302
replace region="SCA"    if countrycode==254
replace region="Africa"    if countrycode==409
replace region="OWH"    if countrycode==252
replace region="Asia"    if countrycode==602
replace region="SCA"    if countrycode==201
replace region="OWH"    if countrycode==277
replace region="Asia"    if countrycode==614
replace region="Europe"    if countrycode==358
replace region="Africa"    if countrycode==402
replace region="Europe"    if countrycode==320
replace region="SCA"    if countrycode==202
replace region="OWH"    if countrycode==266
replace region="OWH"    if countrycode==266
replace region="Asia"    if countrycode==603
replace region="Europe"    if countrycode==351
replace region="Africa"    if countrycode==447
replace region="Asia"    if countrycode==606
replace region="Africa"    if countrycode==403
replace region="Africa"    if countrycode==401
replace region="Africa"    if countrycode==450
replace region="Asia"    if countrycode==607
replace region="Africa"    if countrycode==404
replace region="North America"    if countrycode==100
replace region="Europe"    if countrycode==323
replace region="Asia"    if countrycode==655
replace region="OWH"    if countrycode==266
replace region="Africa"    if countrycode==405
replace region="Africa"    if countrycode==406
replace region="Europe"    if countrycode==327
replace region="SCA"    if countrycode==203
replace region="Asia"    if countrycode==650
replace region="Asia"    if countrycode==655
replace region="Asia"    if countrycode==610
replace region="Asia"    if countrycode==601
replace region="SCA"    if countrycode==204
replace region="Africa"    if countrycode==456
replace region="Africa"    if countrycode==408
replace region="Africa"    if countrycode==407
replace region="Asia"    if countrycode==620
replace region="Asia"    if countrycode==601
replace region="SCA"    if countrycode==205
replace region="Africa"    if countrycode==417
replace region="Europe"    if countrycode==359
replace region="OWH"    if countrycode==206
replace region="OWH"    if countrycode==275
replace region="Europe"    if countrycode==304
replace region="Europe"    if countrycode==364
replace region="Europe"    if countrycode==305
replace region="Africa"    if countrycode==412
replace region="OWH"    if countrycode==269
replace region="OWH"    if countrycode==207
replace region="OWH"    if countrycode==268
replace region="SCA"    if countrycode==208
replace region="Africa"    if countrycode==410
replace region="SCA"    if countrycode==209
replace region="Europe"    if countrycode==327
replace region="Africa"    if countrycode==438
replace region="Africa"    if countrycode==457
replace region="Europe"    if countrycode==331
replace region="Africa"    if countrycode==411
replace region="Asia"    if countrycode==609
replace region="OWH"    if countrycode==268
replace region="Europe"    if countrycode==305
replace region="Asia"    if countrycode==632
replace region="Europe"    if countrycode==306
replace region="OWH"    if countrycode==255
replace region="Europe"    if countrycode==307
replace region="OWH"    if countrycode==255
replace region="SCA"    if countrycode==256
replace region="OWH"    if countrycode==255
replace region="Asia"    if countrycode==609
replace region="Asia"    if countrycode==610
replace region="Asia"    if countrycode==610
replace region="Asia"    if countrycode==609
replace region="Africa"    if countrycode==413
replace region="Africa"    if countrycode==414
replace region="Middle East"    if countrycode==504
replace region="Europe"    if countrycode==337
replace region="Europe"    if countrycode==308
replace region="Africa"    if countrycode==415
replace region="Europe"    if countrycode==309
replace region="Europe"    if countrycode==327
replace region="Europe"    if countrycode==310
replace region="Europe"    if countrycode==311
replace region="OWH"    if countrycode==265
replace region="OWH"    if countrycode==255
replace region="SCA"    if countrycode==210
replace region="Europe"    if countrycode==327
replace region="Africa"    if countrycode==416
replace region="Africa"    if countrycode==430
replace region="SCA"    if countrycode==257
replace region="OWH"    if countrycode==211
replace region="Asia"    if countrycode==601
replace region="SCA"    if countrycode==212
replace region="Asia"    if countrycode==611
replace region="Europe"    if countrycode==354
replace region="Europe"    if countrycode==312
replace region="Asia"    if countrycode==612
replace region="Asia"    if countrycode==613
replace region="Middle East"    if countrycode==502
replace region="Middle East"    if countrycode==503
replace region="Europe"    if countrycode==313
replace region="Europe"    if countrycode==327
replace region="Middle East"    if countrycode==504
replace region="Europe"    if countrycode==314
replace region="OWH"    if countrycode==258
replace region="Europe"    if countrycode==320
replace region="Asia"    if countrycode==614
replace region="Europe"    if countrycode==327
replace region="Middle East"    if countrycode==505
replace region="Asia"    if countrycode==609
replace region="Europe"    if countrycode==338
replace region="Africa"    if countrycode==418
replace region="Asia"    if countrycode==655
replace region="Asia"    if countrycode==626
replace region="Europe"    if countrycode==366
replace region="Middle East"    if countrycode==506
replace region="Europe"    if countrycode==339
replace region="Asia"    if countrycode==615
replace region="Europe"    if countrycode==332
replace region="Middle East"    if countrycode==507
replace region="Africa"    if countrycode==419
replace region="Africa"    if countrycode==420
replace region="Africa"    if countrycode==421
replace region="Europe"    if countrycode==315
replace region="Europe"    if countrycode==333
replace region="Europe"    if countrycode==316
replace region="Asia"    if countrycode==616
replace region="Europe"    if countrycode==360
replace region="Africa"    if countrycode==422
replace region="Europe"    if countrycode==321
replace region="Africa"    if countrycode==423
replace region="Asia"    if countrycode==617
replace region="Asia"    if countrycode==633
replace region="Africa"    if countrycode==424
replace region="Europe"    if countrycode==317
replace region="Asia"    if countrycode==658
replace region="OWH"    if countrycode==255
replace region="Africa"    if countrycode==425
replace region="Africa"    if countrycode==453
replace region="Africa"    if countrycode==456
replace region="SCA"    if countrycode==213
replace region="Asia"    if countrycode==657
replace region="Europe"    if countrycode==340
replace region="Europe"    if countrycode==318
replace region="Asia"    if countrycode==651
replace region="Europe"    if countrycode==361
replace region="OWH"    if countrycode==266
replace region="Africa"    if countrycode==426
replace region="Africa"    if countrycode==427
replace region="Africa"    if countrycode==437
replace region="Asia"    if countrycode==635
replace region="Asia"    if countrycode==618
replace region="Europe"    if countrycode==319
replace region="OWH"    if countrycode==259
replace region="OWH"    if countrycode==277
replace region="Asia"    if countrycode==610
replace region="Asia"    if countrycode==620
replace region="SCA"    if countrycode==214
replace region="Africa"    if countrycode==428
replace region="Africa"    if countrycode==429
replace region="Asia"    if countrycode==620
replace region="Asia"    if countrycode==601
replace region="Asia"    if countrycode==652
replace region="Europe"    if countrycode==327
replace region="Europe"    if countrycode==320
replace region="Middle East"    if countrycode==508
replace region="Asia"    if countrycode==622
replace region="Asia"    if countrycode==659
replace region="SCA"    if countrycode==215
replace region="Asia"    if countrycode==619
replace region="Asia"    if countrycode==650
replace region="SCA"    if countrycode==216
replace region="SCA"    if countrycode==217
replace region="Asia"    if countrycode==623
replace region="Asia"    if countrycode==604
replace region="Europe"    if countrycode==355
replace region="Europe"    if countrycode==321
replace region="SCA"    if countrycode==204
replace region="Middle East"    if countrycode==510
replace region="OWH"    if countrycode==273
replace region="Asia"    if countrycode==609
replace region="Europe"    if countrycode==356
replace region="Europe"    if countrycode==341
replace region="Africa"    if countrycode==432
replace region="OWH"    if countrycode==277
replace region="Asia"    if countrycode==637
replace region="Europe"    if countrycode==322
replace region="Africa"    if countrycode==451
replace region="Europe"    if countrycode==314
replace region="Middle East"    if countrycode==511
replace region="Europe"    if countrycode==327
replace region="Africa"    if countrycode==433
replace region="Europe"    if countrycode==362
replace region="Africa"    if countrycode==454
replace region="Europe"    if countrycode==327
replace region="Europe"    if countrycode==314
replace region="Africa"    if countrycode==434
replace region="Asia"    if countrycode==625
replace region="OWH"    if countrycode==276
replace region="Europe"    if countrycode==365
replace region="Europe"    if countrycode==363
replace region="Asia"    if countrycode==654
replace region="Africa"    if countrycode==435
replace region="Africa"    if countrycode==436
replace region="OWH"    if countrycode==268
replace region="OWH"    if countrycode==268
replace region="OWH"    if countrycode==268
replace region="Africa"    if countrycode==458
replace region="Europe"    if countrycode==323
replace region="Asia"    if countrycode==650
replace region="Asia"    if countrycode==608
replace region="OWH"    if countrycode==255
replace region="OWH"    if countrycode==277
replace region="Africa"    if countrycode==455
replace region="OWH"    if countrycode==267
replace region="OWH"    if countrycode==270
replace region="OWH"    if countrycode==263
replace region="OWH"    if countrycode==271
replace region="Africa"    if countrycode==441
replace region="SCA"    if countrycode==260
replace region="Europe"    if countrycode==320
replace region="Africa"    if countrycode==442
replace region="Europe"    if countrycode==324
replace region="Europe"    if countrycode==325
replace region="Middle East"    if countrycode==512
replace region="Asia"    if countrycode==628
replace region="Europe"    if countrycode==342
replace region="Africa"    if countrycode==443
replace region="Asia"    if countrycode==629
replace region="Asia"    if countrycode==660
replace region="Africa"    if countrycode==444
replace region="Asia"    if countrycode==620
replace region="Asia"    if countrycode==638
replace region="OWH"    if countrycode==266
replace region="OWH"    if countrycode==261
replace region="Asia"    if countrycode==609
replace region="Africa"    if countrycode==445
replace region="Europe"    if countrycode==326
replace region="Europe"    if countrycode==343
replace region="OWH"    if countrycode==266
replace region="Asia"    if countrycode==656
replace region="Africa"    if countrycode==446
replace region="OWH"    if countrycode==268
replace region="Africa"    if countrycode==455
replace region="OWH"    if countrycode==266
replace region="Asia"    if countrycode==605
replace region="Asia"    if countrycode==604
replace region="Europe"    if countrycode==344
replace region="Middle East"    if countrycode==513
replace region="Europe"    if countrycode==327
replace region="North America"    if countrycode==199
replace region="SCA"    if countrycode==218
replace region="Europe"    if countrycode==345
replace region="Asia"    if countrycode==636
replace region="Europe"    if countrycode==330
replace region="SCA"    if countrycode==219
replace region="Asia"    if countrycode==653
replace region="Europe"    if countrycode==327
replace region="Asia"    if countrycode==610
replace region="Middle East"    if countrycode==504
replace region="OWH"    if countrycode==268
replace region="Africa"    if countrycode==440
replace region="Middle East"    if countrycode==514
replace region="Middle East"    if countrycode==500
replace region="Europe"    if countrycode==328
replace region="Africa"    if countrycode==448
replace region="Africa"    if countrycode==431

				* Region codes
				ge regioncode=0
				replace regioncode=1 if region=="North America"
				replace regioncode=2 if region=="OWH"
				replace regioncode=3 if region=="SCA"
				replace regioncode=4 if region=="Europe"
				replace regioncode=5 if region=="Africa"
				replace regioncode=6 if region=="Middle East"
				replace regioncode=7 if region=="Asia"

rename country country_bea

*Make detailed regions for Aisa
gen region_det=region
	replace region_det="Central Asia" if country_n=="Kazakhstan"
	replace region_det="Central Asia" if country_n=="Tajikistan"
	replace region_det="Central Asia" if country_n=="Uzbekistan"
	replace region_det="Central Asia" if country_n=="Turkmenistan"
	replace region_det="Central Asia" if country_n=="Kyrgyzstan"
	
	replace region_det="East Asia" if country_n=="China"
	replace region_det="East Asia" if country_n=="Mongolia"
	replace region_det="East Asia" if country_n=="Korea, North"
	replace region_det="East Asia" if country_n=="Korea, South"
	replace region_det="East Asia" if country_n=="Japan"
	replace region_det="East Asia" if country_n=="Hong Kong"
	replace region_det="East Asia" if country_n=="Taiwan"
	replace region_det="East Asia" if country_n=="Macao"
	
	replace region_det="South Asia" if country_n=="India"
	replace region_det="South Asia" if country_n=="Bangladesh"
	replace region_det="South Asia" if country_n=="Sri Lanka"
	replace region_det="South Asia" if country_n=="Afghanistan"
	replace region_det="South Asia" if country_n=="Pakistan"
	replace region_det="South Asia" if country_n=="Bhutan"
	replace region_det="South Asia" if country_n=="Nepal"
	replace region_det="South Asia" if country_n=="Maldives"
	
	replace region_det="Southeast Asia" if country_n=="Brunei"
	replace region_det="Southeast Asia" if country_n=="Cambodia"
	replace region_det="Southeast Asia" if country_n=="Thailand"
	replace region_det="Southeast Asia" if country_n=="Philippines"
	replace region_det="Southeast Asia" if country_n=="Indonesia"
	replace region_det="Southeast Asia" if country_n=="Laos"
	replace region_det="Southeast Asia" if country_n=="Malaysia"
	replace region_det="Southeast Asia" if country_n=="Myanmar"
	replace region_det="Southeast Asia" if country_n=="Singapore"
	replace region_det="Southeast Asia" if country_n=="Timor-Leste"
	replace region_det="Southeast Asia" if country_n=="Vietnam"
	replace region_det="Southeast Asia" if country_n=="Christmas Island"
	replace region_det="Southeast Asia" if country_n=="Cocos (Keeling) Island"
	
	replace region_det="Western Asia" if country_n=="Georgia"
	replace region_det="Western Asia" if country_n=="Armenia"
	replace region_det="Western Asia" if country_n=="Azerbaijan"
	replace region_det="Western Asia" if country_n=="Turkey"
	*replace region_det="Western Asia" if country_n=="Cyprus"
	replace region_det="Western Asia" if country_n=="Syria"
	replace region_det="Western Asia" if country_n=="Lebanon"
	replace region_det="Western Asia" if country_n=="Israel"
	replace region_det="Western Asia" if country_n=="Gaza Strip Administered by Israel"
	replace region_det="Western Asia" if country_n=="West Bank administered by Israel"
	replace region_det="Western Asia" if country_n=="Jordan"
	replace region_det="Western Asia" if country_n=="Iraq"
	replace region_det="Western Asia" if country_n=="Iran"
	replace region_det="Western Asia" if country_n=="Kuwait"
	replace region_det="Western Asia" if country_n=="Bahrain"
	replace region_det="Western Asia" if country_n=="Saudi Arabia"
	replace region_det="Western Asia" if country_n=="Qatar"
	
	replace region_det="Oceania" if country_n=="Australia"
	replace region_det="Oceania" if country_n=="New Zealand"
	replace region_det="Oceania" if country_n=="New Caledonia"
	replace region_det="Oceania" if country_n=="Vanuatu"
	replace region_det="Oceania" if country_n=="Papua New Guinea"
	replace region_det="Oceania" if country_n=="Solomon Islands"
	replace region_det="Oceania" if region=="Asia" & region_det=="Asia"  //  just all these little islands left
	
	replace region_det="NMS" if country_n=="Latvia"
	replace region_det="NMS" if country_n=="Lithuania"
	replace region_det="NMS" if country_n=="Estonia"
	replace region_det="NMS" if country_n=="Bulgaria"
	replace region_det="NMS" if country_n=="Poland"
	replace region_det="NMS" if country_n=="Hungary"
	replace region_det="NMS" if country_n=="Cyprus"
	replace region_det="NMS" if country_n=="Czech Republic"
	replace region_det="NMS" if country_n=="Malta"
	replace region_det="NMS" if country_n=="Romania"
	replace region_det="NMS" if country_n=="Slovakia"
	replace region_det="NMS" if country_n=="Slovenia"
	
tab region_det if region=="Asia"	
tab country_name if region_det=="Asia"
tab region_det if region=="Europe"
tab country_n if region_det~="Europe" & region=="Europe"
	
save $data/country_data_clean.dta, replace

*Make a bea country code mapping
contract countrycode country_bea region regioncode region_det
  drop _freq
  keep if countrycode~=. | country_bea~=""
  
  save $data/bea_lfttd_country_conc.dta, replace
				
				
* END--------------------------------------------------------------
